package com.itboye.mengwu.app.ui.mymeng;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.login.ui.LoginActivity;
import com.itboye.mengwu.app.ui.aiwujia.YuErJingMomDetailActivity;
import com.itboye.mengwu.data.model.MePingLunbean;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.presenter.MePingLunpter;
import com.itboye.mengwu.luntan.comment.adapter.MepingLunAdapter;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

//import com.itboye.core.imageview.RoundImage;

public class MeCommentActivity extends BaseActivity implements Observer,
		IXListViewListener {

	private Button btn_comment, btn_reply;
	private EditText edt_reply;
	private static final int ONE_COMMENT_CODE = -1;

	private MepingLunAdapter adapter;

	/** Item数据实体集合 */
	/** ListView对象 */
	private XListView mePingLun;
	private ImageView onPinZanback;
	MePingLunpter presenter;
	String uid;
	ArrayList<MePingLunbean.MyPingLun> arrlist = new ArrayList<MePingLunbean.MyPingLun>();
	MePingLunbean lunbean;
	private TextView txtMsg;
	private int pageNo = 1;
	RelativeLayout nodata;
	String authorId;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_pinglunme;
	}

	@Override
	public void onMyCreate() {
		adapter = new MepingLunAdapter(this, arrlist);
		mePingLun.setAdapter(adapter);
		txtMsg.setText("我的评论");
		mePingLun.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				// Log.e("hu --Status",
				// arrlist.get(position-1).getThread_info().getStatus());
				// Log.e("hu --tid", arrlist.get(position-1).getTid());
				// Log.e("hu --posion", position-1 +",size=" +arrlist.size() +
				// "");
				if (arrlist.get(position - 1).getThread_info().getStatus()
						.equals("-1")) {
					ByAlert.alert("该贴已删除");
					return;
				}
				if (arrlist.get(position - 1).getThread_info().getFid()
						.equals("4")) {
					Intent intent = new Intent(MeCommentActivity.this,
							WoDeDongTaiDetail2.class);
					intent.putExtra("tid", arrlist.get(position - 1).getTid());
					startActivity(intent);
				} else if (arrlist.get(position - 1).getThread_info().getFid()
						.equals("6")
						|| arrlist.get(position - 1).getThread_info().getFid()
								.equals("7")
						|| arrlist.get(position - 1).getThread_info().getFid()
								.equals("8")) {// 育儿经
					Intent intent2 = new Intent(MeCommentActivity.this,
							YuErJingMomDetailActivity.class);
					intent2.putExtra("tid", arrlist.get(position - 1).getTid());
					startActivity(intent2);

				}
			}
		});
		initCommentData();

	}

	public boolean judgeLogin() {
		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
		if (uid == null || uid.equals("")) {

			Intent intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == MePingLunpter.MePingLunSuccessEvent) {
				if (pageNo == 1)
					arrlist.clear();
				arrlist.addAll(((MePingLunbean) entity.getData()).getList());

				adapter.notifyDataSetChanged();
				if (arrlist.size() <= 0) {
					nodata.setVisibility(View.VISIBLE);
					mePingLun.setVisibility(View.GONE);
				} else {
					nodata.setVisibility(View.GONE);
					mePingLun.setVisibility(View.VISIBLE);
				}

			} else if (entity.getEventType() == MePingLunpter.MePingLunSuccessEvent) {

				presenter.getInfo(lunbean);
			} else if (entity.getEventType() == MePingLunpter.MePingLunSuccessEvent) {
				ByAlert.alert("评论失败" + entity.getData());
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception

		}
		stopLoad();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageNo = 1;
		lunbean.setPageNo(pageNo + "");
		presenter.getInfo(lunbean);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		pageNo++;
		lunbean.setPageNo(pageNo + "");
		presenter.getInfo(lunbean);
	}

	public void stopLoad() {
		mePingLun.stopLoadMore();
		mePingLun.stopRefresh();
	}

	public void onPinZanback(View view) {
		this.finish();
	}

	public void initCommentData() {
		mePingLun.setXListViewListener(this);
		mePingLun.setPullLoadEnable(true);
		mePingLun.setPullRefreshEnable(true);
		showProgressDialog("数据加载中，请稍后", true);
		uid = SPUtils.get(MeCommentActivity.this, null, SpPublicParams.UID, "")
				+ "";
		presenter = new MePingLunpter(this);
		lunbean = new MePingLunbean();
		lunbean.setUid(uid);
		lunbean.setPageNo(pageNo + "");
		presenter.getInfo(lunbean);
	}

}
